import { Component, Input, ViewChild, ElementRef, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { environment } from '../../../../../../../../environments/environment';
import { UtilsService } from '../../../../../../../shared/services/utils.service';

import * as _ from 'lodash';
import { CompanyRiskRatingService } from '../../../companyRiskRating.service';
import { Subscription } from 'rxjs';
import { NgxSmartModalService } from 'ngx-smart-modal';
import { AnalyticsService } from '../../../../../../../shared/services/googleAnalytics.service';
import { GoogleAnalyticsBeacon } from '../../../../../../../shared/models/googleAnalytics.model';

@Component({
    selector: 'pillar-details',
    templateUrl: './pillarDetails.component.html',
    styleUrls: ['./pillarDetails.component.scss',
    '../../../../shared/companyIndicator/companyIndicator.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class PillarDetailsComponent {
    private sub: Subscription;
    public expanded: boolean = false;
    public readonly resourcesUrl: string = environment.resourcesUrl;
    public getUrlFriendlyName: Function = UtilsService.getUrlFriendlyName;
    public pillarDetails: any;

    @Input() indicatorIndex: number;
    @Input() canExpand: boolean;
    @Input() indicator: any;
    @Input() showAxis: boolean;
    @Input() columns: any[];
    @Input() subTitle: string;

    @Input() companyId: string;
    @Input() externalId: string;

    @ViewChild('panelLink') panelLink: ElementRef;

    constructor(
        public ngxSmartModalService: NgxSmartModalService,
        private cdRef: ChangeDetectorRef,
        private route: ActivatedRoute,
        private riskService: CompanyRiskRatingService,
        private analyticsService: AnalyticsService) {
    }

    public loadDetails(code: string) {
        this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('ESG RR Pillars', 'ESG Risk Rating', { CompanyId: this.companyId, ExternalId: this.externalId, Code: code }));
        this.sub = this.riskService.getPillarDetails(this.companyId, code).subscribe((data) => {
            this.pillarDetails = data;
            this.cdRef.markForCheck();
        });

        this.ngxSmartModalService.getModal('indicatorPillar' + this.getCleanIndicatorCode(code)).open();
    }

    ngOnDestroy() {
        if (this.sub) {
            this.sub.unsubscribe();
        }
    }

    get displayCode() {
        return this.indicator.code.substring(0, 2) !== 'CG';
    }

    public formatValue(indicator: any, column: any): string {
        let value: number = column.hasNoValue == true ? indicator[column.valueField] : indicator[column.valueField].value;
        if (value == undefined) value = 0;
        if (column.formatMethod) value = column.formatMethod(value);
        return value.toFixed(column.decimals).toString() + ' ' + column.ratingUnit;
    }

    public getCleanIndicatorCode( code ) {
        return UtilsService.getCleanIndicatorCode(code);
    }
}